package ar.edu.unq.wannaplay.panels;

import java.util.LinkedList;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;

public class WPListViewPanel<T> extends ServicedPanel<T> {

    private static final long serialVersionUID = 1L;

    private List<String> labels;

    public WPListViewPanel(final String id, final T service, final String[] labels) {
        super(id, service);
        this.setLabels(this.generateListFromArray(labels));
    }

    private List<String> generateListFromArray(final String[] manyLabels) {
        List<String> fkngPMD = new LinkedList<String>();
        for (String fkg : manyLabels) {
            fkngPMD.add(fkg);
        }
        return fkngPMD;
    }

    protected Label prepareHeaderLabel(final String id) {
        return this.buildFancyLabel(id, this.prepareHeaders());
    }

    protected Label prepareFieldsLabel(final String id) {
        return this.buildFancyLabel(id, this.prepareFields());
    }

    private Label buildFancyLabel(final String id, final String content) {
        Label label = new Label(id, content);
        label.setEscapeModelStrings(false);
        return label;
    }

    private String prepareHeaders() {
        return this.buildFancyHtmlContent("th", "", "");
        // return this.buildFancyHtmlContent("th",
        // "<wicket:message key=\"header.panel.", "\"/>");
    }

    private String prepareFields() {
        return this.buildFancyHtmlContent("td", "<span wicket:id=\"", "\"></span>");
    }

    private String buildFancyHtmlContent(final String tag, final String fancyStart, final String fancyEnd) {
        List<String> items = this.getLabels();
        StringBuilder builder = new StringBuilder(1024);
        int columnSize = 100 / items.size();
        for (String header : items) {
            builder.append("<");
            builder.append(tag);
            builder.append(" width=\"");
            builder.append(columnSize);
            builder.append("\">");
            builder.append(fancyStart);
            builder.append(this.getLocalizer().getString("header.panel." + header, this));
            builder.append(fancyEnd);
            builder.append("</");
            builder.append(tag);
            builder.append(">");
        }
        return builder.toString();
    }

    public List<String> getLabels() {
        return labels;
    }

    public void setLabels(final List<String> labels) {
        this.labels = labels;
    }
}
